﻿using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Quadrant.domain.Models;
using Quadrant.domain.Services;
using Quadrant.web.Helpers;

namespace Quadrant.web.API
{
    [Authorize]
    public class SticksController : BaseApiController
    {
        [HttpGet]
        [ResponseType(typeof(Stick[]))]
        public async Task<IHttpActionResult> Get()
        {
            using (var svr = new StickServices(this.Logger))
            {
                return Ok(await svr.GetSticks(this.UserId));
            }
        }

        [HttpGet]
        [ResponseType(typeof(Stick))]
        public async Task<IHttpActionResult> Get(long id)
        {
            using (var svr = new StickServices(this.Logger))
            {
                return Ok(await svr.GetStick(id));
            }
        }

        [ResponseType(typeof(void))]
        [HttpPut]
        public async Task<IHttpActionResult> PutStick([FromUri]long id, [FromBody]BoardPosition position)
        {
            if (position != null)
            {
                using (var svr = new StickServices(this.Logger))
                {
                    return Ok(await svr.SavePositionStick(id,position));
                }
            }
            return StatusCode(HttpStatusCode.NoContent);
        }

        [HttpPost]
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> Save(Stick value)
        {
            if (value != null)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                using (var svr = new StickServices(this.Logger))
                {
                    return Ok(await svr.SaveStick(value));
                }
            }
            return NotFound();
        }

        [HttpDelete]
        [ResponseType(typeof(bool))]
        public async Task<IHttpActionResult> Delete([FromUri]long id)
        {
            if (id >= 0)
            {
                using (var svr = new StickServices(this.Logger))
                {
                    return Ok(await svr.DeleteStick(id));
                }
            }
            return NotFound();
        }
    }
}
